package com.iweb.userOrder.dao;

import com.iweb.userOrder.model.Orders;
import com.iweb.util.JdbcUtil;
import com.iweb.util.PageModel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class OrdersDaoImpl implements OrdersDao {
    @Override
    public List<Orders> selectByPage(PageModel pageModel) {
        List<Orders> orders = new ArrayList<>();
        List<Map<String, Object>> list = null;
        String likeValue = pageModel.getLikeValue()==null?"":pageModel.getLikeValue();
        StringBuffer sql = new StringBuffer("select order_id,dish_name,dish_price,date_format(order_time,'%Y-%m-%d %H:%i:%s') order_time" +
                " from orders where order_id like ? or dish_name like ? or dish_price like ?");
        //查询总条数
        int recordCount = JdbcUtil.jdbcBySelect(sql.toString(), "%" + likeValue + "%", "%" + likeValue + "%", "%" + likeValue + "%").size();
        pageModel.setRecordCount(recordCount);
        int startCount = (pageModel.getPageNo() - 1) * pageModel.getPageSize();

        //分页查询
        sql.append(" order by order_id desc limit ? , ?");
        list = JdbcUtil.jdbcBySelect(sql.toString(), "%" + likeValue + "%", "%" + likeValue + "%", "%" + likeValue + "%",
                startCount, pageModel.getPageSize());
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                Map<String, Object> map = list.get(i);
                Integer order_id = (Integer) map.get("order_id");
                String dish_name = (String) map.get("dish_name");
                Integer dish_price = (Integer) map.get("dish_price");
                String order_time = (String) map.get("order_time");
                Orders orders1 = new Orders(order_id, dish_name,  dish_price, order_time);
                orders.add(orders1);
            }
        }
        return orders;
    }

    @Override
    public int updateOne(Orders orders) {
        System.out.println("执行更新操作，参数：" + orders);
        String sql = "update orders set dish_name=? ,dish_price=? "
                + " where order_id=?";
        int result = JdbcUtil.jdbcByUpdate(sql,orders.getDish_name(),
                orders.getDish_price(),orders.getOrder_id());
        System.out.println("更新结果：" + result);
        return result;
    }

    @Override
    public int addOne(Orders orders) {
        System.out.println("=== 开始执行新增订单 ===");
        System.out.println("订单对象: " + orders);
        String sql = "insert into orders(dish_name, dish_price, order_time) values(?, ?, now())";
        System.out.println("SQL语句: " + sql);
        System.out.println("参数1(dish_name): " + orders.getDish_name());
        System.out.println("参数2(dish_price): " + orders.getDish_price());
        int result = JdbcUtil.jdbcByUpdate(sql, orders.getDish_name(), orders.getDish_price());
        System.out.println("新增结果: " + result);
        System.out.println("=== 新增订单执行完成 ===");
        return result;
    }

    @Override
    public Orders queryById(String order_id) {
        System.out.println("开始查询订单，ID: " + order_id);
        String sql = "select order_id,dish_name,dish_price,date_format(order_time,'%Y-%m-%d %H:%i:%s') order_time " +
                " from orders where order_id=?";
        List<Map<String,Object>> list = JdbcUtil.jdbcBySelect(sql,order_id);
        System.out.println("查询结果列表: " + list);
        if (list == null || list.isEmpty()) {
            System.out.println("未找到订单数据");
            return null;
        }
        Map<String,Object> map = list.get(0);
        System.out.println("查询结果Map: " + map);
        String dish_name = (String) map.get("dish_name");
        String order_time = (String) map.get("order_time");
        Integer dish_price = (Integer) map.get("dish_price");
        Orders orders = new Orders(Integer.valueOf(order_id),dish_name,dish_price,order_time);
        System.out.println("构造的Orders对象: " + orders);
        return orders;
    }

    @Override
    public int deleteById(String order_id) {
        String sql = "delete from orders where order_id=?";
        return JdbcUtil.jdbcByUpdate(sql, order_id);
    }
}
